package cn.zbh.org.service.impl;

import cn.zbh.basic.exception.BusinessException;
import cn.zbh.basic.service.impl.BasicServiceImpl;
import cn.zbh.basic.util.BaiduAuditUtils;
import cn.zbh.org.domain.Employee;
import cn.zbh.org.domain.Shop;
import cn.zbh.org.domain.ShopLog;
import cn.zbh.org.dto.ShopDto;
import cn.zbh.org.mapper.EmployeeMapper;
import cn.zbh.org.mapper.ShopLogMapper;
import cn.zbh.org.mapper.ShopMapper;
import cn.zbh.org.service.IEmployeeService;
import cn.zbh.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopLogMapper logMapper;

    @Autowired
    private JavaMailSender javaMailSender;


    @Override
    public void settlement(Shop shop) {
        // 实现shop入驻流程
        // 1. 非空校验  spring中提供了一个类，处理string的
        if(StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAdmin().getUsername())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            // 提示店铺商家，参数不能为空
            throw new BusinessException("参数不能为空");
        }

        // 通过百度AI审核名称是否符合要求
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){ // 名称有问题
            throw new BusinessException("店铺名称违规！");
        }


        Employee admin = shop.getAdmin();
        // 2. 两次密码不一致校验
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致，请重新输入！");
        }

        // 3. 店铺是否注册过校验
        Shop dbShop = shopMapper.getShopGetByMassge(shop);
        if(dbShop!=null){
            throw new BusinessException("该店铺已经被注册过！");
        }

        // 把shop和employee数据保存在表中
        // 1. 保存employee, 获取自增的主键
        employeeMapper.save(admin);

        // 2. 把自增的主键设置到shop表中admin_id中
        shop.setAdmin_id(admin.getId());
        // 3. 保存shop数据，返回自增的主键
        shopMapper.add(shop);

        // 4. 3 返回的自增主键设置给employee的shop_id字段
        admin.setShop_id(shop.getId());
        // 5. 更新employee数据
        employeeMapper.update(admin);

    }

    @Override
    public void pass(ShopLog log) throws MessagingException { // Log:  shop_id,  note
        // 1. 修改shop状态
        // 通过shop_id 查询shop数据  ，再修改状态
        Shop shop = shopMapper.findById(log.getShop_id());
        // 审核通过
        shop.setState(2);
        shopMapper.update(shop);

        // 2. 录入日志  shopLog
        log.setState(2L);
        log.setAudit_id(1L);
        logMapper.add(log);

        // 3. 通知商家   邮件发送
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1912813725@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>",true);

        // 获取收件人
        Employee employee = employeeMapper.findById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("1912813725@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);

    }

    @Override
    public void reject(ShopLog log) throws MessagingException {
// 1. 修改shop状态
        // 通过shop_id 查询shop数据  ，再修改状态
        Shop shop = shopMapper.findById(log.getShop_id());
        // 审核驳回
        shop.setState(4);
        shopMapper.update(shop);

        // 2. 录入日志  shopLog
        log.setState(4L);
        log.setAudit_id(1L);
        logMapper.add(log);

        // 3. 通知商家   邮件发送
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1912813725@qq.com");
        helper.setSubject("驳回邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>",true);

        // 获取收件人
        Employee employee = employeeMapper.findById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("1912813725@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);

        /**
         * 两种情况:
         *    1. 修改状态为驳回  state = 4; 下次入驻的时候判断该店铺是否是被驳回过，如果是就直接继续入驻。
         *    2. 直接删除 shop,  emp
         *
         */
    }

    @Override
    public List<ShopDto> getEcharts() {
        
        return shopMapper.getEcharts();
    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }


}
